Replace queues with Qlist in session handling. (#263)
authortsteven4 <tsteven4@users.noreply.github.com>
Fri, 19 Oct 2018 12:13:51 +0000 (06:13 -0600)
committerGitHub <noreply@github.com>
Fri, 19 Oct 2018 12:13:51 +0000 (06:13 -0600)
session tracks a possible series of input formats and input filenames.

csv_util.cc
defs.h
main.cc
mmo.cc
session.cc
session.h

index d35227e3c77965d2da96793cfe7f6d882595189e..af70bfa5b6095cf02dc3c8fe6d3f3c5b04019ec2 100644 (file)
@@ -2090,10 +2090,10 @@ xcsv_waypt_pr(const Waypoint* wpt)
     break;
     /* specials */
     case XT_FILENAME:
-      buff = QString().sprintf(fmp.printfc.constData(), wpt->session->filename);
+      buff = QString().sprintf(fmp.printfc.constData(), CSTR(wpt->session->filename));
       break;
     case XT_FORMAT:
-      buff = QString().sprintf(fmp.printfc.constData(), wpt->session->name);
+      buff = QString().sprintf(fmp.printfc.constData(), CSTR(wpt->session->name));
       break;
     case -1:
       if (strncmp(fmp.key.constData(), "LON_10E", 7) == 0) {
diff --git a/defs.h b/defs.h
index 2958bc15fbff048dfe596213d22cf790200f7733..0ab96ff0f6fd837a5ce9ed528c8622cb8294064e 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -532,7 +532,7 @@ public:
   float odometer_distance; /* Meters? */
   geocache_data* gc_data;
   format_specific_data* fs;
-  session_t* session;  /* pointer to a session struct */
+  const session_t* session;    /* pointer to a session struct */
   void* extra_data;    /* Extra data added by, say, a filter. */
 
 public:
@@ -660,7 +660,7 @@ public:
   unsigned short cet_converted;        /* strings are converted to UTF8; interesting only for input */
   gb_color line_color;         /* Optional line color for rendering */
   int line_width;         /* in pixels (sigh).  < 0 is unknown. */
-  session_t* session;  /* pointer to a session struct */
+  const session_t* session;    /* pointer to a session struct */
 
 public:
   route_head();
diff --git a/main.cc b/main.cc
index 3776e31fbd929123b27fdd1a439251a0dddddae4..e5b76857f3528afeb1466652e474f89d02c6b853 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -383,7 +383,7 @@ main(int argc, char* argv[])
 
       cet_convert_init(ivecs->encode, ivecs->fixed_encode);    /* init by module vec */
 
-      start_session(ivecs->name, CSTR(fname));
+      start_session(ivecs->name, fname);
       ivecs->rd_init(fname);
       ivecs->read();
       ivecs->rd_deinit();
@@ -607,7 +607,7 @@ main(int argc, char* argv[])
 
     cet_convert_init(ivecs->encode, 1);
 
-    start_session(ivecs->name, CSTR(qargs.at(0)));
+    start_session(ivecs->name, qargs.at(0));
     if (ivecs->rd_init == nullptr) {
       fatal("Format does not support reading.\n");
     }
@@ -671,7 +671,7 @@ main(int argc, char* argv[])
       if (fname.isEmpty()) {
         fatal("An input file (-f) must be specified.\n");
       }
-      start_session(ivecs->name, CSTR(fname));
+      start_session(ivecs->name, fname);
       ivecs->position_ops.rd_init(fname);
     }
 
diff --git a/mmo.cc b/mmo.cc
index 7adb3e67591a07e4810dc5e6aa4cd62203593498..2c55cd63d88ef7c5e96f0911a2f4618005311c19 100644 (file)
--- a/mmo.cc
+++ b/mmo.cc
@@ -1230,7 +1230,7 @@ mmo_write_wpt_cb(const Waypoint* wpt)
   if (cx != nullptr) {
     char* kml = nullptr;
 
-    if (strcmp(wpt->session->name, "kml") == 0) {
+    if (wpt->session->name == QLatin1String("kml")) {
       utf_string tmp(true, cx);
       cx = kml = strip_html(&tmp);
     }
index 6afb27181d6c34af2ea655a224c7d90e3a5207b9..f3be806fe26e2f78aba7e49e04393d8686e883cf 100644 (file)
 #include "defs.h"
 #include "session.h"
 
-static queue session_list;
-static int session_ct;
+#include <QtCore/QList>  // for QList
 
-static void session_free(session_t* s);
+static QList<session_t> session_list;
 
 void
 session_init()
 {
-  QUEUE_INIT(&session_list);
-  session_ct = 0;
+  session_list.clear();
 }
 
 void
 session_exit()
 {
-  queue* elem, *tmp;
-
-  QUEUE_FOR_EACH(&session_list, elem, tmp) {
-    session_t* s = reinterpret_cast<session_t *>(elem);
-    dequeue(&s->Q);
-    session_free(s);
-  }
+  session_list.clear();
 }
 
 void
-start_session(const char* name, const char* filename)
+start_session(const QString& name, const QString& filename)
 {
-  if (session_ct == 0) {
-    QUEUE_INIT(&session_list);
-  }
-  session_ct++;
-
-  session_t* s = (session_t*) xcalloc(1, sizeof(*s));
-  ENQUEUE_TAIL(&session_list, &s->Q);
-  QUEUE_INIT(&s->category_list);
-  s->nr = session_ct;
-  s->name = name;
-  s->filename = xstrdup(filename);
+  session_list.append(session_t(name, filename));
 }
 
-session_t*
+const session_t*
 curr_session()
 {
-  return reinterpret_cast<session_t *>(session_list.prev);
-}
-
-/* in work
-
-int
-session_add_category(const char *name, const int id)
-{
-       queue *elem, *tmp;
-       session_t *s;
-       category_t *c;
-
-       s = curr_session();
-
-       QUEUE_FOR_EACH(&s->category_list, elem, tmp) {
-               c = (category_t *) elem;
-               if (case_ignore_strcmp(c->name, name) == 0) {
-                       if (id >= 0) c->id = id;
-                       return c->id;
-               }
-
-       }
-
-       c = xmalloc(sizeof(*c));
-       c->name = xstrdup(name);
-       if (id < 0) c->id = -(++s->unknown_category_ct);
-       else c->id = id;
-
-       s->category_ct++;
-       ENQUEUE_TAIL(&s->category_list, &c->Q);
-
-       return c->id;
+  if (!session_list.isEmpty()) {
+    return &session_list.last();
+  } else {
+    fatal("Attempt to fetch session outside of session range.");
+  }
 }
-*/
 
 /* non public functions */
 
-static void
-session_free(session_t* s)
-{
-  queue* elem, *tmp;
-  QUEUE_FOR_EACH(&s->category_list, elem, tmp) {
-    category_t* c = reinterpret_cast<category_t *>(elem);
-    dequeue(&c->Q);
-    xfree(c);
-  }
-  xfree(s->filename);
-  xfree(s);
-}
index 1a32385cb564351b8941911608c48fa3a1b1f386..9487b8729a9b9c78bb45ec56c660375863874db8 100644 (file)
--- a/session.h
+++ b/session.h
 #ifndef SESSION_H_INCLUDED_
 #define SESSION_H_INCLUDED_
 
-#include "queue.h"
-
-typedef struct {
-  queue Q;
-  int id;
-  char* name;
-} category_t;
-
-typedef struct {
-  queue Q;
-  int nr;
-  const char* name;            /* in normal case the name of a format */
-  char* filename;                      /* used file within format */
-  int category_ct;
-  int unknown_category_ct;     /* added without id */
-  queue category_list;
-} session_t;
+#include <QtCore/QString>  // for QString
+
+struct session_t {
+public:
+  QString name;                                        /* in normal case the name of a format */
+  QString filename;                    /* used file within format */
+
+public:
+  session_t(const QString& name_p, const QString& filename_p) : name{name_p},filename{filename_p} {}
+};
 
 void session_init(void);
 void session_exit(void);
 
-void start_session(const char* name, const char* filename);
-session_t* curr_session(void);
-
-/* in work
-int session_add_category(const char *name, const int id);
-*/
+void start_session(const QString& name, const QString& filename);
+const session_t* curr_session(void);
 
 #endif  // SESSION_H_INCLUDED_